package org.homerepair

class ClientUser {
	
	static hasMany = [jobs:Job]
	
	transient springSecurityService

	String username
	String password
	boolean enabled
	String firstName
	String lastName
	String email
	String address1
	String address2
	String city
	String state
	int zipCode
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired

	static constraints = {
		username blank: false, unique: true
		password blank: false, password: true
		firstName blank: false
		lastName blank: false
		email blank: false, email: true
		address1 blank: false
		city blank: false
		state inList: ['FL', 'NJ']
		zipCode max: 99999, min: 00000
		
	}
	
	String toString()
	{
		username
	}

	static mapping = {
		password column: '`password`'
	}

	Set<ClientRole> getAuthorities() {
		ClientUserClientRole.findAllByClientUser(this).collect { it.clientRole } as Set
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
}
